#include "Draw.h"

void CreateBmpHeader (RECT &rect, BITMAPINFOHEADER &bmpInfH)
{
	bmpInfH.biSize =			sizeof(BITMAPINFOHEADER);
	bmpInfH.biWidth =			rect.right - rect.left;
	bmpInfH.biHeight=			rect.bottom - rect.top;
	bmpInfH.biPlanes =			1;
	bmpInfH.biBitCount =		32;
	bmpInfH.biCompression =		BI_RGB;
	bmpInfH.biSizeImage =		0;
	bmpInfH.biXPelsPerMeter =	0;
	bmpInfH.biYPelsPerMeter =	0;
	bmpInfH.biClrUsed =			0;
	bmpInfH.biClrImportant =	0;
}


void DrawByMatrix (HDC hdc, BITMAPINFOHEADER &bmpInfH, Life &life)
{
	void*				Bits;
	BITMAPINFO			bmpInf;	
	HBITMAP				hBmp;
	HDC					hCmpDC;

	hCmpDC = CreateCompatibleDC(0);	

	bmpInf.bmiHeader = bmpInfH; 

	hBmp = CreateDIBSection(hdc,&bmpInf,DIB_RGB_COLORS,&Bits,0,0);
	SelectObject(hCmpDC,hBmp);
	for(int y=0;y<bmpInfH.biHeight;y++)
	{
		for(int x=0;x<bmpInfH.biWidth;x++)
			((unsigned*)Bits)[y*bmpInfH.biHeight+x] = BKGRND_COLOR;
	}
	for(int i=0; i<bmpInfH.biHeight; i++)
		for(int j=0; j<bmpInfH.biWidth; j++)
		{
			if (life.life[i/3][j/3]==true && i%3==0 && j%3==0)
			{
				((unsigned*)Bits)[i*bmpInfH.biWidth+j] = CELL_COLOR;
				((unsigned*)Bits)[i*bmpInfH.biWidth+j+1] = CELL_COLOR;
				((unsigned*)Bits)[i*bmpInfH.biWidth+j+2] = CELL_COLOR;

				((unsigned*)Bits)[i*bmpInfH.biWidth+j+bmpInfH.biWidth] = CELL_COLOR;
				((unsigned*)Bits)[i*bmpInfH.biWidth+j+2+bmpInfH.biWidth] = CELL_COLOR;

				((unsigned*)Bits)[i*bmpInfH.biWidth+j+bmpInfH.biWidth*2] = CELL_COLOR;
				((unsigned*)Bits)[i*bmpInfH.biWidth+j+1+bmpInfH.biWidth*2] = CELL_COLOR;
				((unsigned*)Bits)[i*bmpInfH.biWidth+j+2+bmpInfH.biWidth*2] = CELL_COLOR;
			}		
		}

	BitBlt(hdc, 0, 0, bmpInfH.biWidth, bmpInfH.biHeight,
		hCmpDC, 0, 0, SRCCOPY);	
	DeleteDC(hCmpDC);
	DeleteObject(hBmp);
}